

package jetbrains.buildServer.server.rest.model;

import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * @author Yegor.Yarko
 * Date: 11/11/2017
 */

public interface ItemsProviders {
  interface ItemsRetriever<T> {
    @Nullable
    List<T> getItems();

    Integer getCount();

    boolean isCountCheap();

    @Nullable
    PagerData getPagerData();
  }

  abstract class ItemsProvider<T> {
    @NotNull
    public abstract List<T> getItems(@Nullable final String locator);

    @Nullable
    public Integer getCheapCount(@Nullable final String locator) {
      return null;
    }

    @NotNull
    public static <S> ItemsProvider<S> items(@NotNull Function<String, List<S>> getItems) {
      return new ItemsProvider<S>() {
        @NotNull
        @Override
        public List<S> getItems(@Nullable final String locator) {
          return getItems.apply(locator);
        }
      };
    }

    @NotNull
    public static <S> ItemsProvider<S> items(@NotNull List<S> items) {
      return new ItemsProvider<S>() {
        @NotNull
        @Override
        public List<S> getItems(@Nullable final String locator) {
          return items;
        }

        @Override
        public Integer getCheapCount(@Nullable final String locator) {
          return items.size();
        }
      };
    }
  }
}